import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class EchoSever {

    private DatagramSocket datagramSocket = null;

    public EchoSever(int p) throws SocketException {
        datagramSocket = new DatagramSocket(p);
    }

    public void start() throws IOException {

        System.out.println("服务器启动");
        while (true) {

            byte[] resqut = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(resqut,1024   );
            datagramSocket.receive(datagramPacket);
            String r = new String(datagramPacket.getData(),0,datagramPacket.getLength());

            String resp = poccess(r);

            DatagramPacket datagramPacket1 = new DatagramPacket(resp.getBytes(),resp.getBytes().length,datagramPacket.getSocketAddress());
            datagramSocket.send(datagramPacket1);
            System.out.printf("[%s:%d] req: %s, resp: %s\n", datagramPacket.getAddress().toString(),datagramPacket.getPort(),r , resp);
        }

    }

    private String poccess(String r) {
        return r;
    }

    public static void main(String[] args) throws IOException {
        Thread thread = new Thread(() -> {
            System.out.println("hello");
        });

        thread.start();
        EchoSever sever = new EchoSever(9099);
        sever.start();


    }
}


